package com.css.dzj.dzjap.modules.lawenforcesuper.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.ApDeptExtractInfoEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.ApDeptExtractService;
import com.css.dzj.dzjap.modules.lawenforcesuper.view.ApDeptExtractView;
import com.css.dzj.dzjap.modules.publick.entity.ApPublicEntity;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 安评单位抽取信息controller
 * @author limanman
 */
@RequestMapping("/apDeptExtract")
@RestController
public class ApDeptExtractController {
    @Autowired
    private ApDeptExtractService apDeptExtractService;
    @Autowired
    BaseService baseService;
    /**
     * 添加两张表
     * @param apDeptExtractInfoEntity
     * @return
     */
    @PostMapping("/saveApDeptExtract")
    public R saveApDeptExtract(@RequestBody ApDeptExtractInfoEntity apDeptExtractInfoEntity, HttpServletRequest request){
        try {
            // apDeptExtractInfoEntity.setTaskNum(3);
            R r=apDeptExtractService.saveApDeptExtract(apDeptExtractInfoEntity,request);

            if("500".equals(r.get("code").toString())){
                return  R.error(r.get("msg").toString());
            }
            baseService.logSuccess(3,"添加成功","2");
            return  R.ok(r).put("taskId",r.get("taskId"));

        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3,"添加失败","2");
            return  R.error(e.getMessage());
        }

    }

    /**
     * 查询所有的抽取任务
     * @param taskTitle
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/queryExtract")
        public R queryExtract(@RequestParam(required = false) String taskTitle,
                                                      @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                      @RequestParam(required = false, defaultValue = "10") Integer pageSize,HttpServletRequest request){
        try {
            PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
            QueryResults<ApDeptExtractInfoEntity> apDeptExtractInfoEntities = apDeptExtractService.queryExtract(pageRequest,taskTitle,request);
            baseService.logSuccess(3,"查询成功","2");
            return R.ok().put("apDeptExtractInfoEntities",apDeptExtractInfoEntities);
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3,"查询失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 通过任务id查询抽取的安评单位
     * @param taskId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/queryApUnitByTaskId")
    public R queryApUnitByTaskId(@RequestParam(required = false) String taskId,
                                                  @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,HttpServletRequest request){
        try {
            PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
            QueryResults<ApDeptExtractView> apDeptExtractViews = apDeptExtractService.queryApUnitByTaskId(pageRequest,taskId,request);
            ApDeptExtractInfoEntity apDeptExtractInfoEntity = apDeptExtractService.queryExtractInfoById(taskId);
            baseService.logSuccess(3,"查询成功","2");
            return R.ok().put("apDeptExtractViews",apDeptExtractViews).put("apDeptExtractInfoEntity",apDeptExtractInfoEntity);
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3,"查询失败","2");
            return R.error(e.getMessage());
        }
    }

    /***
     * 根据任务ID找任务
     * @param taskId
     * @return
     */
    @PostMapping("/getById")
    public R findById(String taskId){
       ApDeptExtractInfoEntity apDeptExtractInfoEntity= apDeptExtractService.queryExtractInfoById(taskId);
       baseService.logSuccess(3,"查询成功","2");
       return R.ok().put("data",apDeptExtractInfoEntity);
    }

    /***
     * 拟制公告
     * @param apPublicEntity
     * @return
     */
    @PostMapping("/addPublic")
    public R addPublic(@RequestBody ApPublicEntity apPublicEntity,HttpServletRequest request){
        try {
            if (StringUtils.isEmpty(apPublicEntity)) {
                return R.error("公告为空");
            }
            apDeptExtractService.addPulic(apPublicEntity, request);
            baseService.logSuccess(3,"添加成功","2");
            return R.ok();
        } catch(Exception e) {
            baseService.logFailed(3,"添加失败","2");
            return R.error(e.getMessage());
        }

    }
    /**
     * 根据任务id查询抽取的安评单位ApUnit
     * @param taskId
     * @return
     */
    @PostMapping("/findDeptByTaskId")
    public R findDeptByTaskId(String taskId){
        if (StringUtils.isEmpty(taskId)) {
            return R.error("任务ID不能为空");
        }
        List list = apDeptExtractService.findDeptByTaskId(taskId);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data",list);
    }

}
